//
//  SetUserInformationViewController.m
//  FlowerRainGround
//
//  Created by lanou on 16/4/20.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "SetUserInformationViewController.h"
#import "LoginViewController.h"
#import "PersonInformationViewController.h"
#import "CountManagerViewController.h"
#import "ScoreViewController.h"
#import <UIImageView+WebCache.h>
@interface SetUserInformationViewController ()<UIScrollViewDelegate>

//五个button下面的view
@property (strong, nonatomic) IBOutlet UIView *UserView;
@property (strong, nonatomic) IBOutlet UIButton *cancelCache;

//退出按钮
@property (strong, nonatomic) IBOutlet UIButton *cancelButton;


@end

@implementation SetUserInformationViewController

//点击用户实现的方法
- (IBAction)UserInforMation:(id)sender {
    
    PersonInformationViewController *personVC = [[PersonInformationViewController alloc] init];
    [self.navigationController pushViewController:personVC animated:YES];
}

//点击账号管理实现的方法
- (IBAction)CountManager:(id)sender {
    
    CountManagerViewController *countVC = [[CountManagerViewController alloc] init];
    [self.navigationController pushViewController:countVC animated:YES];
}

//点击积分管理实现的方法
- (IBAction)Score:(id)sender {
    ScoreViewController *scoreVC = [[ScoreViewController alloc] init];
    [self.navigationController pushViewController:scoreVC animated:YES];
}


//显示缓存的大小
- (void)getCacheSize {
    //获取缓存的大小
   CGFloat size = [SDImageCache sharedImageCache].getSize / 1000.0/ 1000;
    [self.cancelCache setTitle:[NSString stringWithFormat:@"    清除缓存（已使用%.2fMB)",size] forState:UIControlStateNormal];
}


//获取缓存的大小
- (void)getSize
{
    //图片大小，
    NSFileManager *manager = [NSFileManager defaultManager];
    
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *path = [cachePath stringByAppendingPathComponent:@"default/com.hackemist.SDWebImageCache.default"];
    
    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
    NSInteger totalsize = 0;
    //遍历上面的文件
    for (NSString *name in fileEnumerator) {
        NSString *filePath = [path stringByAppendingPathComponent:name];
        
        //判断是否是我们要的文件，还是文件夹
        NSDictionary *dic = [manager attributesOfItemAtPath:filePath error:nil];
        if ([dic[NSFileType] isEqualToString:NSFileTypeRegular]) {
            
            totalsize += [dic[NSFileSize] integerValue];//文件大小
            
        }
        
    }

}

//点击清除缓存实现的方法
- (IBAction)cancelCache:(id)sender {
    
    //清除缓存
    [[SDImageCache sharedImageCache] clearDisk];

    //自己清除,直接将文件夹删除
    //[NSFileManager defaultManager] removeItemAtPath:<#(nonnull NSString *)#> error:nil];
}


//点击退出实现的方法
- (IBAction)cancelLogin:(id)sender {
    if (![[userManager getUsrID] isEqualToString:@" "] ) {
        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"田园生活" message:@"退出当前应用" preferredStyle:UIAlertControllerStyleAlert];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [alertVC dismissViewControllerAnimated:YES completion:nil];
        }]];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            //当有用户的信息时删除用户ID， 退到登陆界面
            [userManager cancelUserID];
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self presentViewController:loginVC animated:YES completion:nil];
        }]];
        [self presentViewController:alertVC animated:YES completion:nil];
    }
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    [self getCacheSize];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"设置";
    self.navigationController.navigationBar.translucent = NO;

    self.UserView.layer.cornerRadius = 5;
    self.UserView.layer.borderWidth = 1;
    self.cancelButton.layer.cornerRadius = 5;
    self.cancelButton.layer.borderWidth = 1;
    
    
    
    // Do any additional setup after loading the view from its nib.
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"1111");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
